package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanFactory;

import beans.Bookmark;
import beans.User;

import formbeans.BookmarkForm;

import model.BookmarkDAO;
import model.Model;
import model.UserDAO;

/*
 *  Course: S09-15437 - WEB APPLICATION DEVELOPMENT 
 * 	Name: Jiale Huang
 * 	Andrew ID: jialeh
 * 	Date: 2/23/2009
 *  Assignment: Hw4
 */


public class ListBookmarkAction extends Action{

	private BookmarkDAO bookmarkDAO;
	private UserDAO userDAO;
	
	public ListBookmarkAction(Model model){
		
		bookmarkDAO = model.getBookmarkDAO();
		userDAO = model.getUserDAO();
	}	
	
	@Override
	public String getName() {
		return "listBookmarks.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);
		
		try{
			List<User> users = userDAO.getUsers();

			if(users.size() != 0){
			
				request.setAttribute("users", users);
				
				User user;
				String bemail = request.getParameter("bemail");
				
				if(bemail == null){
					
					user = users.get(0);
				}else{
					
					user = userDAO.lookup(bemail);
				}
				
				List<Bookmark> bookmarks = bookmarkDAO.getUserBookmarks(user.getUserEmail());
				
				request.setAttribute("viewingUser", user.getFName() + " " + user.getLName());//user.getFName() + user.getLName());
				request.setAttribute("bookmarks", bookmarks);
			}else{
				
				request.setAttribute("message", "There's no user there!!!");
			}
			
			request.setAttribute("page", "b");
			return "index.jsp";
			
		}catch(DAOException e){
			errors.add(e.toString());
			return "index.jsp";
		}
	}
	
	

}
